set linesize 140
set rmsg on
set scheme s1mono
/******************************************************************************
	Project		:	Army Service in the All-Volunteer Era
	Author(s)	:	Kyle Greenberg	(kyle.greenberg@westpoint.edu)
					Matthew Gudgeon (matthew.gudgeon@westpoint.edu)
					Adam Isen 		(Adam.Isen@treasury.gov)
					Corbin Miller 	(Corbin.Miller@treasury.gov)
					Rich Patterson 	(rich_patterson@byu.edu)
	File Name	:	sumstats.do
	Description	:	This file creates descriptive figures and tables.
*******************************************************************************/

*---- if not running master, set up file structure ----*
*change directory to where programs and subfolders are stored
cd ""

if "${raw}"==""		global raw		"raw/"
if "${data}"==""	global data		"data/"
if "${output}"==""	global output	"output/"

cap mkdir	"${output}"
cap mkdir	"${output}sum/"

*---- select subprograms ----*
local fam_inc_hist			1
local median_fam_inc		1
local complier_means_cohort_regs	1
local complier_means_race_regs		1
local complier_means_figs	1
local complier_means_tabs	1
local retest_rates			1
local sumstats_tab			1

*----------------------------------*
*---- family income histograms ----*
*----------------------------------*
if `fam_inc_hist'==1{

	use "${raw}parent-income-random-parentSSN-before", clear
	append using "${raw}parent-income-army-parentSSN-before"
	
	replace tot_inc_lim = tot_inc_lim/1000
	
	*overall
	foreach x in random army{
		sum tot_inc_lim if `x'==1, d
		local mean_tot_inc_lim_`x': di %5.2f r(mean)
		local median_tot_inc_lim_`x': di %5.2f r(p50)
	}
	tw 	(hist tot_inc_lim if random==1, frac start(0) w(5.55))	///
		(hist tot_inc_lim if army==1, frac start(0) w(5.55) fc(none) lc(black)),	///
		legend(	///
			order(1 - "(Mean=`mean_tot_inc_lim_random', Median=`median_tot_inc_lim_random')"	///
			2 - "(Mean=`mean_tot_inc_lim_army', Median=`median_tot_inc_lim_army')")	///
			label(1 "Random Sample") label(2 "Application Sample")	///
			c(1) ring(0) pos(2)	)	///
		ylabel(0(.05).15, angle(0)) yscale(r(0 .16)) graphregion(color(white))	///
		title("Distribution of Family Income, Birth Year 1983-1993") xtitle("Family Income at Age 16 (thousands)") 
	graph export "${output}sum/parent-income-parentSSN.pdf", replace
	tw 	(hist tot_inc_lim if random==1, frac start(0) w(5.55))	///
		(hist tot_inc_lim if army==1, frac start(0) w(5.55) fc(none) lc(black)),	///
		legend(	///
			order(1 - "(Mean=`mean_tot_inc_lim_random', Median=`median_tot_inc_lim_random')"	///
			2 - "(Mean=`mean_tot_inc_lim_army', Median=`median_tot_inc_lim_army')")	///
			label(1 "Random Sample") label(2 "Application Sample")	///
			c(1) ring(0) pos(2)	)	///
		ylabel(0(.05).15, angle(0)) yscale(r(0 .16)) graphregion(color(white))	///
		xtitle("Family Income at Age 16 (thousands)") 
	graph export "${output}sum/parent-income-parentSSN-notitle.pdf", replace
		
	*by race
	use "${raw}parent-income-army-before", clear
	merge 1:m ssn using "${raw}mepcom-treasury", nogen keep(1 3) keepusing(black white)

	replace tot_inc_lim = tot_inc_lim/1000

	foreach x in black white{
		sum tot_inc_lim if `x'==1, d
		local mean_tot_inc_lim_`x': di %5.2f r(mean)
		local median_tot_inc_lim_`x': di %5.2f r(p50)
	}
	tw 	(hist tot_inc_lim if black==1, frac start(0) w(5.55))	///
		(hist tot_inc_lim if white==1, frac start(0) w(5.55) fc(none) lc(black)),	///
		legend(	///
			order(1 - "(Mean=`mean_tot_inc_lim_black', Median=`median_tot_inc_lim_black')"	///
			2 - "(Mean=`mean_tot_inc_lim_white', Median=`median_tot_inc_lim_white')")	///
			label(1 "Black") label(2 "White")	///
			c(1) ring(0) pos(2)	)	///
		ylabel(0(.05).15, angle(0)) yscale(r(0 .16)) graphregion(color(white))	///
		title("Distribution of Family Income, By Race") xtitle("Family Income at Age 16 (thousands)") 
	graph export "${output}sum/parent-income-race.pdf", replace
}	//end if
else di "*---- skipping family income histograms ----*"

*-----------------------------------------------------------*
*---- median family income at 16 by first afqt and race ----*
*-----------------------------------------------------------*
if `median_fam_inc'==1{
	use "${data}army-treasury-analysis", clear
	merge m:1 ssn using "${raw}parent-income-army-before", nogen keep(1 3)
	collapse (median) tot_inc, by(white black firstafqt)
	tw (line tot_inc firstafqt if black==1, lcolor(black) lp(shortdash) lw(medthick))	///
		(line tot_inc firstafqt if white==1, lcolor(gs7) lp(longdash) lw(medthick)),	///
		xtitle(First AFQT Score on File) xlabel(0(10)100) xmtick(0(1)100)	///
		ytitle("Median Parent Income at Age 16 (Dollars)") ylabel(,angle(0) format(%7.0fc))	///
		legend(label(2 "White") label(1 "Black")) xline(30.5 49.5, lcolor(black) lpattern(dot))
	graph export "${output}sum/median-parent-income-by-AFQT-by-race.pdf", replace
}	//end if
else di "*---- skipping median family income figures ----*"

*----------------------------------------------*
*---- complier means by application cohort ----*
*----------------------------------------------*
if `complier_means_cohort_regs'==1{
	foreach cov in age_days educ_hsdip hi_educ poor_share1990 singleparent_share1990 south {
		*initialize output file
		clear 
		set obs 14
		gen b31 = .
		gen se31 = .
		gen b50 = .
		gen se50 = . 
		gen grp = "9092" 
		replace grp = "9395" if _n==2 | _n == 9
		replace grp = "9698" if _n==3 | _n == 10
		replace grp = "9901" if _n==4 | _n == 11
		replace grp = "0204" if _n==5 | _n == 12
		replace grp = "0508" if _n==6 | _n == 13
		replace grp = "0911" if _n==7 | _n == 14
		gen black = _n <= 7
		gen white = _n >= 8
		save "${output}sum/cohortcomp_`cov'.dta" , replace
		
		*bring in the data
		use pid hor_state age_days educ_hsdip educ_some_coll educ_coll_grad access firstyear inst31 k31 k31_2 instk31 instk31_2 inst50 k50 k50_2 instk50 instk50_2 quarterFE firstafqt all white black using "${data}army-treasury-analysis", clear
		merge 1:1 pid using "${raw}Chetty_Mobility_v1", keep(1 3) nogen keepusing(poor_share1990 singleparent_share1990)
	
		gen south = inlist(hor_state,"DE","MD","DC","VA","WV","KY","NC","TN","SC")	///
			| inlist(hor_state,"GA","AL","FL","MS","AR","LA","OK","TX")
		gen hi_educ = (educ_some_coll + educ_coll_grad)
		
		keep `cov' k31 k31_2 instk31 instk31_2 quarterFE access inst31 firstafqt black white all

		gen aX`cov'9092 = access*`cov' if inrange(firstyear,1990,1992) 
		gen aX`cov'9395 = access*`cov' if inrange(firstyear,1993,1995) 
		gen aX`cov'9698 = access*`cov' if inrange(firstyear,1996,1998) 
		gen aX`cov'9901 = access*`cov' if inrange(firstyear,1999,2001) 
		gen aX`cov'0204 = access*`cov' if inrange(firstyear,2002,2004) 
		gen aX`cov'0508 = access*`cov' if inrange(firstyear,2005,2008) 
			
		foreach g in 9092 9395 9698 9901 0204 0508 {
			ivregress 2sls aX`cov'`g' k31 k31_2 instk31 instk31_2 i.quarterFE (access=inst31) if  inrange(firstafqt,12,49) & black == 1, robust
			local a = _b[access]
			local sea = _se[access]
			ivregress 2sls aX`cov'`g' k31 k31_2 instk31 instk31_2 i.quarterFE (access=inst31) if  inrange(firstafqt,12,49) & white == 1, robust
			local b = _b[access]
			local seb = _se[access]
			ivregress 2sls aX`cov'`g' k50 k50_2 instk50 instk50_2 i.quarterFE (access=inst50) if  inrange(firstafqt,31,68) & black ==1, robust
			local c = _b[access]
			local sec = _se[access]
			ivregress 2sls aX`cov'`g' k50 k50_2 instk50 instk50_2 i.quarterFE (access=inst50) if  inrange(firstafqt,31,68) & white ==1, robust
			local d = _b[access]
			local sed = _se[access]

			preserve 
				use "${output}sum/cohortcomp_`cov'" , clear
				replace b31 = `a' if grp == "`g'" & black == 1 
				replace se31 = `sea' if grp == "`g'" & black == 1 
				replace b31 = `b' if grp == "`g'" & white == 1 
				replace se31 = `seb' if grp == "`g'" & white == 1 
				replace b50 = `c' if grp == "`g'" & black == 1 
				replace se50 = `sec' if grp == "`g'" & black == 1 
				replace b50 = `d' if grp == "`g'" & white == 1 
				replace se50 = `sed' if grp == "`g'" & white == 1 
				save "${output}sum/cohortcomp_`cov'" , replace
			restore
		}
	}
}	//end if
else di "*---- skipping complier means regressions ----*"

*----------------------------------------------*
*---- complier means by race ----*
*----------------------------------------------*
if `complier_means_race_regs'==1{
	foreach cov in male age_days educ_inhs lo_educ educ_hsdip hi_educ tot_inc_lim northeast midwest south west poor_share1990 poor_share2000 emp2000 med_hhinc1990 popdensity2000 singleparent_share1990 {
		*bring in the data
		use pid ssn hor_state male age_days educ_inhs educ_lessthanhs educ_ged educ_hsdip educ_some_coll educ_coll_grad access firstyear inst31 k31 k31_2 instk31 instk31_2 inst50 k50 k50_2 instk50 instk50_2 quarterFE firstafqt all white black using "${data}army-treasury-analysis", clear
		merge 1:1 pid using "${raw}Chetty_Mobility_v1", keep(1 3) nogen keepusing(poor_share1990 poor_share2000 emp2000 med_hhinc1990 popdensity2000 singleparent_share1990)
		merge 1:1 ssn using "${raw}parent-income-army-before", keep(1 3) nogen keepusing(tot_inc_lim)

		gen northeast = inlist(hor_state,"ME","NH","MA","RI","VT","NY","CT","NJ","PA")
		gen midwest = inlist(hor_state,"OH","MI","IN","IL","WI","IA","MN","MO")	///
			| inlist(hor_state,"ND","SD","NE","KS")
		gen south = inlist(hor_state,"DE","MD","DC","VA","WV","KY","NC","TN","SC")	///
			| inlist(hor_state,"GA","AL","FL","MS","AR","LA","OK","TX")
		gen west = inlist(hor_state,"MT","WY","CO","NM","ID")	///
			| inlist(hor_state,"UT","AZ","WA","OR","NV","CA")

		gen lo_educ = (educ_lessthanhs + educ_ged)
		gen hi_educ = (educ_some_coll + educ_coll_grad)
		
		keep `cov' inst31 k31 k31_2 instk31 instk31_2 inst50 k50 k50_2 instk50 instk50_2 quarterFE access firstyear firstafqt black white all

		gen aX`cov' = access*`cov'
		foreach strata in black white{
			ivregress 2sls aX`cov' k31 k31_2 instk31 instk31_2 i.quarterFE (access=inst31) if inrange(firstyear,1990,2011)  &  inrange(firstafqt,12,49) & `strata' == 1, robust
			local b_`strata'_31_`cov' = _b[access]
			sum `cov' if e(sample)==1
			local m_`strata'_31_`cov' = r(mean)

			eststo i: ivregress 2sls aX`cov'  k50 k50_2 instk50 instk50_2 i.quarterFE (access=inst50) if inrange(firstyear,1990,2011) & inrange(firstafqt,31,68) & `strata' ==1, robust
			local b_`strata'_50_`cov' = _b[access]
			sum `cov' if e(sample)==1
			local m_`strata'_50_`cov' = r(mean)
		}
	}
	clear 
	set obs 1
	foreach x in b m {
		foreach strata in black white {
			foreach cut in 31 50 {
				foreach cov in `controllist_s' {
					di "`x'_`strata'_`cut'_`cov' = ``x'_`strata'_`cut'_`cov''"
					gen `x'_`strata'_`cut'_`cov' = ``x'_`strata'_`cut'_`cov''
				}
			}
		}
	}
	save "${output}sum/complier_means_race", replace

}	//end if
else di "*---- skipping complier means by race regressions ----*"

if `complier_means_figs'==1{
	foreach cov in age_days educ_hsdip hi_educ poor_share1990 singleparent_share1990 south {
		if "`cov'" == "age_days" local ytitlab "Age (Complier Mean)"
		if "`cov'" == "educ_hsdip" local ytitlab "High School Dip. (Complier Mean)"
		if "`cov'" == "hi_educ" local ytitlab "Some College + (Complier Mean)"
		if "`cov'" == "poor_share1990" local ytitlab "Poverty Share (Complier Mean)"
		if "`cov'" == "singleparent_share1990" local ytitlab "Single Parent Share (Complier Mean)"
		if "`cov'" == "south" local ytitlab "South (Complier Mean)"
		
		use "${output}sum/cohortcomp_`cov'" , clear

		g b31_b_ = b31 if black 
		g b31_w_ = b31 if white 
		g se31_b_ = se31 if black 
		g se31_w_ = se31 if white 
		g b50_b_ = b50 if black 
		g b50_w_ = b50 if white 
		g se50_b_ = se50 if black 
		g se50_w_ = se50 if white 

		g cohort = .
		replace cohort = 1990 if grp == "9092"
		replace cohort = 1993 if grp == "9395"
		replace cohort = 1996 if grp == "9698"
		replace cohort = 1999 if grp == "9901"
		replace cohort = 2002 if grp == "0204"
		replace cohort = 2005 if grp == "0508"
		replace cohort = 2008 if grp == "0911"

		foreach g in b w {
			g ub31_`g'_ = b31_`g'_  + 1.96*se31_`g'_ 
			g lb31_`g'_  = b31_`g'_  - 1.96*se31_`g'_ 

			g ub50_`g'_  = b50_`g'_  + 1.96*se50_`g'_ 
			g lb50_`g'_  = b50_`g'_  - 1.96*se50_`g'_ 
		}

		g cohort2 = cohort + 0.1
		replace cohort = cohort - 0.1

		drop if cohort > 2007
		
		foreach cut in 31 50 {
			tw (scatter b`cut'_b_ cohort,  mcolor(gs7)) (rcap ub`cut'_b_ lb`cut'_b_ cohort, lpattern(longdash) lcolor(gs7)) ///
				(scatter b`cut'_w_ cohort2, mcolor(black)) (rcap ub`cut'_w_ lb`cut'_w_ cohort2, lpattern(solid) lcolor(black)) ///
				,  ytitle("`ytitlab'")  xtitle("Application Cohort")  xtick(1990(3)2005) xlabel(1990 "90-92" 1993 "93-95" 1996 "96-98" 1999 "99-01" 2002 "02-04" 2005 "05-08" ) legend(order(1 3) label(1 "Black (Cutoff = `cut')") label(3 "White (Cutoff = `cut')")) 
			graph export "${output}sum/cohortcomp_`cov'_`cut'.pdf", as(pdf) replace
		}
	}
}	//end if
else di "*---- skipping complier means figures ----*"

*---- complier characteristics, by race ----*
if `complier_means_tabs'==1{
	*labels
	local male_l			"Male"
	local age_days_l		"Age"
	local educ_inhs_l 		"In High School"
	local lo_educ_l			"No HS Diploma"
	local educ_hsdip_l		"HS Diploma"
	local hi_educ_l			"Some College+"
	local tot_inc_lim_l		"Family Income"
	local northeast_l		"Northeast"
	local midwest_l			"Midwest"
	local south_l			"South"
	local west_l			"West"
	local poor_share1990_l	"1990 Poverty Rate"
	local poor_share2000_l	"2000 Poverty Rate"
	local emp2000_l			"2000 Employment Rate"
	local med_hhinc1990_l	"1990 Median HH Income"
	local popdensity2000_l	"2000 Population Density"
	local singleparent_share1990_l	"1990 Single Parent Share"
	
	foreach x in male age_days educ_inhs lo_educ educ_hsdip hi_educ northeast midwest south west poor_share1990 poor_share2000 emp2000 singleparent_share1990{
		local `x'_f %5.3f
	}
	foreach x in tot_inc_lim med_hhinc1990 popdensity2000{
		local `x'_f %9.0fc
	}

	use "${output}sum/complier_means_race", clear
	
	file open s using "${output}sum/complier_means_race.tex", write replace
	file write s "\begin{tabular}{l*{8}c}" _n "\hline\hline" _n
	file write s "& \multicolumn{4}{c}{31-Cutoff} &   \multicolumn{4}{c}{50-Cutoff} \\" _n
	file write s "& \multicolumn{2}{c}{Black} & \multicolumn{2}{c}{White} & \multicolumn{2}{c}{Black} & \multicolumn{2}{c}{White} \\" _n
	file write s "& sample & complier & sample & complier & sample & complier & sample & complier \\" _n
	file write s "& mean & mean & mean & mean & mean & mean & mean & mean \\" _n
	file write s "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8)  \\ \cline{2-9} \\" _n
	file write s "\emph{Demographics at time of application} \\" _n
	foreach cov in male age_days educ_inhs lo_educ educ_hsdip hi_educ tot_inc_lim {
		use "${output}sum/cohortcomp_`cov'" , clear
		file write s "``cov'_l' "
		foreach cut in 31 50 {
			foreach race in black white {
				file write s "&" ``x'_f' (m_`race'_`cut'_`cov') "&" ``x'_f' (b_`race'_`cut'_`cov')
			}
		}
		file write s "\\" _n
	}
	file write s "\emph{Region} \\" _n
	foreach cov in northest midwest south west {
		use "${output}sum/cohortcomp_`cov'" , clear
		file write s "``cov'_l' "
		foreach cut in 31 50 {
			foreach race in black white {
				file write s "&" ``x'_f' (m_`race'_`cut'_`cov') "&" ``x'_f' (b_`race'_`cut'_`cov')
			}
		}
		file write s "\\" _n
	}
	file write s "\emph{County Characteristics} \\" _n
	foreach cov in northest midwest south west {
		use "${output}sum/cohortcomp_`cov'" , clear
		file write s "``cov'_l' "
		foreach cut in 31 50 {
			foreach race in black white {
				file write s "&" ``x'_f' (m_`race'_`cut'_`cov') "&" ``x'_f' (b_`race'_`cut'_`cov')
			}
		}
		file write s "\\" _n
	}
	file write s "\hline \hline" _n "\end{tabular}" _n

}	//end if
else di "*---- skipping complier means by race tables ----*"

*----------------------*
*---- retest rates ----*
*----------------------*
if `retest_rates'==1{
	use "${data}army-treasury-analysis", clear
	
	merge 1:1 pid using "${raw}mepcom-treasury", nogen keep(1 3) keepusing(firstafqtdt hist_afqt_pctl_1 retest retest_2 afqt_pctl)
	
	gen firstafqt_yr = year(firstafqtdt)
	gen secondafqt = hist_afqt_pctl_1 if retest == 1 & retest_2 == 1
	replace secondafqt = afqt_pctl if retest == 1 & retest_2 == 0

	preserve
		keep if inrange(firstafqt_yr,1990,2011)
		keep if inrange(firstafqt,26,30)
		eststo clear
		estpost summ retest retest_2
		est sto s1
		estpost summ retest retest_2 if black == 1
		est sto s2
		estpost summ retest retest_2 if white == 1
		est sto s3
		estpost summ retest retest_2 if hisp == 1
		est sto s4
		
		esttab s1 s2 s3 s4 using "${output}sum/retest_cutoff31_by_race.tex" , ///
			cell(mean(fmt(3))) ///
			replace noobs nomtitle nodepvar nonumber plain fragment label ///
			substitute(_ \_) style(tex) ///
			mlabels(, none) collabels(, none) varlabels(retest "Retested at Least Once" retest_2 "Retested at Least Twice") ///
			stats(N , labels("\hline \\ Observations") fmt(0))                            
	restore

	eststo clear
	preserve
		keep if inrange(firstafqt_yr,1990,2011)
		keep if inrange(secondafqt,26,30)
		estpost summarize retest_2
		est sto s1
		estpost summarize retest_2 if black == 1
		est sto s2
		estpost summarize retest_2 if white == 1
		est sto s3
		estpost summarize retest_2 if hisp == 1
		est sto s4

		esttab s1 s2 s3 s4 using "${output}sum/retest2_cutoff31_by_race.tex" , ///
			cell(mean(fmt(3))) ///
			replace noobs nomtitle nodepvar nonumber plain fragment label ///
			substitute(_ \_) style(tex) ///
			mlabels(, none) collabels(, none) varlabels(retest_2 "Retested at Least Twice") ///
			stats(N , labels("\hline \\ Observations") fmt(0))							
	restore

	eststo clear
	preserve
		keep if inrange(firstafqt_yr,1990,2011)
		keep if inrange(firstafqt,45,49)
		estpost summarize retest retest_2
		est sto s1
		estpost summarize retest retest_2 if black == 1
		est sto s2
		estpost summarize retest retest_2 if white == 1
		est sto s3
		estpost summarize retest retest_2 if hisp == 1
		est sto s4

		esttab s1 s2 s3 s4 using "${output}sum/retest_cutoff50_by_race.tex" , ///
			cell(mean(fmt(3))) ///
			replace noobs nomtitle nodepvar nonumber plain fragment label ///
			substitute(_ \_) style(tex) ///
			mlabels(, none) collabels(, none) varlabels(retest "Retested at Least Once" retest_2 "Retested at Least Twice") ///
			stats(N , labels("\hline \\ Observations") fmt(0))
	restore

	eststo clear
	preserve
		keep if inrange(firstafqt_yr,1990,2011)
		keep if inrange(secondafqt,45,49)
		estpost summarize retest_2
		est sto s1
		estpost summarize retest_2 if black == 1
		est sto s2
		estpost summarize retest_2 if white == 1
		est sto s3
		estpost summarize retest_2 if hisp == 1
		est sto s4

		esttab s1 s2 s3 s4 using "${output}sum/retest2_cutoff50_by_race.tex" , ///
			cell(mean(fmt(3))) ///
			replace noobs nomtitle nodepvar nonumber plain fragment label ///
			substitute(_ \_) style(tex) ///
			mlabels(, none) collabels(, none) varlabels(retest_2 "Retested at Least Twice") ///
			stats(N , labels("\hline \\ Observations") fmt(0))
	restore

}	//end if
else di "*---- skipping retest rates ----*"

*----------------------------*
*---- summary statistics ----*
*----------------------------*	
if `sumstats_tab'==1{
	local varlist access years_served age_days firstafqt male white black hisp educ_inhs lo_educ educ_hsdip hi_educ
	local incomelist wages anywages anyf1098T anyf1098 military anyf1040 married

	foreach strata in all white black{
		use "${data}army-treasury-analysis" if `strata'==1, clear
		foreach y in 1 10 19{
			local yt = `y'
			foreach cov of local incomelist {
				*all applicants
				sum `cov'_`yt'
				local m_`strata'_`cov'_`yt'_all = r(mean)
				local N_`strata'_`cov'_`yt'_all = r(N)
				*all analysis sample
				sum `cov'_`yt' if inrange(firstafqt,12,68)
				local m_`strata'_`cov'_`yt' = r(mean)
				local N_`strata'_`cov'_`yt' = r(N)
				*analysis sample, access==1
				sum `cov'_`yt' if inrange(firstafqt,12,68) & access==1
				local m_`strata'_`cov'_`yt'_access = r(mean)
				local N_`strata'_`cov'_`yt'_access = r(N)
				*analysis sample, access==0
				sum `cov'_`yt' if inrange(firstafqt,12,68) & access==0
				local m_`strata'_`cov'_`yt'_noaccess = r(mean)
				local N_`strata'_`cov'_`yt'_noaccess = r(N)
			}
			sum tax_yr_`yt' if inrange(firstafqt,12,68)
			local N_`strata'_`yt' = r(N)
		}
		foreach cov of local varlist{
			*all applicants
			sum `cov'
			local m_`strata'_`cov'_all = r(mean)
			local N_`strata'_`cov'_all = r(N)
			*all analysis sample
			sum `cov' if inrange(firstafqt,12,68)
			local m_`strata'_`cov' = r(mean)
			local N_`strata'_`cov' = r(N)
			*analysis sample, access==1
			sum `cov' if inrange(firstafqt,12,68) & access==1
			local m_`strata'_`cov'_access = r(mean)
			local N_`strata'_`cov'_access = r(N)
			*analysis sample, access==0
			sum `cov' if inrange(firstafqt,12,68) & access==0
			local m_`strata'_`cov'_noaccess = r(mean)
			local N_`strata'_`cov'_noaccess = r(N)
		}

		clear
		set obs 1
		gen n=1
		foreach y in 1 10 19{
			di "`y'"
			local yt = `y'
			foreach cov of local incomelist{
				di "`cov'"
				foreach x in _all "" _access _noaccess {
					gen m_`strata'_`cov'_`yt'`x' = `m_`strata'_`cov'_`yt'`x''
					gen N_`strata'_`cov'_`yt'`x' = `N_`strata'_`cov'_`yt'`x''
				}
			}
			gen N_`strata'_`yt' = `N_`strata'_`yt''
		}
		foreach cov of local varlist{
			di "`cov'"
			foreach x in _all "" _access _noaccess {
				gen m_`strata'_`cov'`x' = `m_`strata'_`cov'`x''
				gen N_`strata'_`cov'`x' = `N_`strata'_`cov'`x''
			}
		}
		save "${output}sum/sumstats-`strata'", replace
	}

	use "${output}sum/sumstats-all", clear
	merge 1:1 n using "${output}sum/sumstats-black", nogen
	merge 1:1 n using "${output}sum/sumstats-white", nogen

	file open s using "${output}sum/sumstats.tex", write replace
	file write s "\begin{tabular}{l*{6}c}" _n "\hline\hline" _n
	file write s "& All & Analysis & Black & White & Enlisted & Did Not Enlist \\" _n
	file write s "& Applicants & Sample & (in A.S.) & (in A.S.) & (in A.S.) & (in A.S.) \\" _n
	file write s "& (1) & (2) & (3) & (4) & (5) & (6) \\ \cline{2-7}" _n
	
	*---- at application/time invariant demographics ----*
	local access_l 			"Accessed Military"
	local years_served_l	"Years Served"
	local age_days_l		"Age"
	local firstafqt_l 		"First AFQT Score"
	local male_l			"Male"
	local white_l 			"White (Non-Hispanic)"
	local black_l 			"Black (Non-Hispanic)"
	local hisp_l 			"Hispanic"
	local educ_inhs_l 		"In High School"
	local lo_educ_l			"No HS Diploma"
	local educ_hsdip_l		"HS Diploma"
	local hi_educ_l			"Some College+"

	file write s "\\ & \multicolumn{6}{c}{\underline{A. At Application}} \\" _n "\\" _n
	foreach cov of local varlist{
		file write s "``cov'_l' " 
		file write s "&" %5.3f (m_all_`cov'_all) 
		file write s "&" %5.3f (m_all_`cov') 
		file write s "&" %5.3f (m_black_`cov') 
		file write s "&" %5.3f (m_white_`cov') 
		file write s "&" %5.3f (m_all_`cov'_access)
		file write s "&" %5.3f (m_all_`cov'_noaccess) 
		file write s "\\" _n
	}

	*---- time-varying outcomes ----*
	local wages_l		"Earnings"
	local anywages_l	"Employment"
	local anyf1098T_l	"College Attendence"
	local anyf1098_l	"Homeownership"
	local married_l		"Married"
	foreach x in anyf1098T anywages anyf1098 married {
		local `x'_f %5.3f
	}
	local wages_f %6.0fc

	file write s "\\ & \multicolumn{6}{c}{\underline{B. 1 Year After Application}} \\" _n "\\" _n
	foreach cov of local incomelist{
		file write s "``cov'_l' "
		file write s "&" ``cov'_f' (m_all_`cov'_1_all) 
		file write s "&" ``cov'_f' (m_all_`cov'_1) 
		file write s "&" ``cov'_f' (m_black_`cov'_1) 
		file write s "&" ``cov'_f' (m_white_`cov'_1) 
		file write s "&" ``cov'_f' (m_all_`cov'_1_access) 
		file write s "&" ``cov'_f' (m_all_`cov'_1_noaccess) 
		file write s "\\" _n
	}

	file write s "\\ & \multicolumn{6}{c}{\underline{C. 10 Years After Application}} \\" _n "\\" _n
	foreach cov of local incomelist{
		file write s "``cov'_l' "
		file write s "&" ``cov'_f' (m_all_`cov'_10_all) 
		file write s "&" ``cov'_f' (m_all_`cov'_10) 
		file write s "&" ``cov'_f' (m_black_`cov'_10) 
		file write s "&" ``cov'_f' (m_white_`cov'_10) 
		file write s "&" ``cov'_f' (m_all_`cov'_10_access) 
		file write s "&" ``cov'_f' (m_all_`cov'_10_noaccess) 
		file write s "\\" _n
	}

	file write s "\\ & \multicolumn{6}{c}{\underline{D. 19 Years After Application}} \\" _n "\\" _n
	foreach cov of local incomelist {
		file write s "``cov'_l' "
		file write s "&" ``cov'_f' (m_all_`cov'_19_all) 
		file write s "&" ``cov'_f' (m_all_`cov'_19) 
		file write s "&" ``cov'_f' (m_black_`cov'_19) 
		file write s "&" ``cov'_f' (m_white_`cov'_19) 
		file write s "&" ``cov'_f' (m_all_`cov'_19_access) 
		file write s "&" ``cov'_f' (m_all_`cov'_19_noaccess) 
		file write s "\\" _n
	}

	file write s "\hline\hline" _n "\end{tabular}" _n

}	//end if
else di "*---- skipping summary statistics table ----*"
